package cmpe.swe.purcom.controller.mobile;

import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cmpe.swe.purcom.model.Community;
import cmpe.swe.purcom.model.Event;
import cmpe.swe.purcom.model.Service;
import cmpe.swe.purcom.model.ServiceContribution;
import cmpe.swe.purcom.model.Skill;
import cmpe.swe.purcom.model.Stuff;
import cmpe.swe.purcom.model.StuffContribution;
import cmpe.swe.purcom.model.User;
import cmpe.swe.purcom.service.CommunityService;
import cmpe.swe.purcom.service.SkillService;
import cmpe.swe.purcom.service.UserService;

@Controller
@RequestMapping("/mobile")
public class MobileController {
	@Autowired
	private UserService userService;

	@Autowired
	private SkillService skillService;

	@Autowired
	private CommunityService communityService;

	/*
	 * USER methods
	 */

	@RequestMapping(value = "/user/signin", method = RequestMethod.GET)
	public @ResponseBody
	User signIn(@RequestParam("email") String email,
			@RequestParam("password") String password) {
		return userService.canLogin(email, password);
	}

	@RequestMapping(value = "/user/signup")
	public @ResponseBody
	User signUp(@RequestBody User u) {
		userService.createUser(u);
		return u;
	}

	/*
	 * SKILL methods
	 */

	@RequestMapping(value = "/skill/list", method = RequestMethod.GET)
	public @ResponseBody
	List<Skill> listSkills() {
		return skillService.getSkills();
	}

	/*
	 * COMMUNITY methods
	 */

	@RequestMapping(value = "/community/list", method = RequestMethod.GET)
	public @ResponseBody
	List<Community> listCommunities() {
		return communityService.getCommunities();
	}

	@RequestMapping(value = "/community/search", method = RequestMethod.GET)
	public @ResponseBody
	List<Community> listCommunities(@RequestBody String keyword) {
		return communityService.searchCommunitiesByName(keyword);
	}

	@RequestMapping(value = "/community/listmine", method = RequestMethod.POST)
	public @ResponseBody
	List<Community> listMyCommunities(@RequestBody Long userId) {
		return communityService.getMyCommunities(userId);
	}
	
	@RequestMapping(value = "/community/new", method = RequestMethod.POST)
	public @ResponseBody
	Community createCommunity(@RequestBody Map<String, Object> jsonObj) {
		Community c = new ObjectMapper().convertValue(jsonObj.get("community"),
				Community.class);
		Long userId = new ObjectMapper().convertValue(jsonObj.get("userID"),
				Long.class);

		return communityService.createCommunity(c, userId);
	}

	@RequestMapping(value = "/community/{id}", method = RequestMethod.GET)
	public @ResponseBody
	Community getCommunityById(@PathVariable Long id) {
		return communityService.getCommunity(id);
	}

	@RequestMapping(value = "/community/{id}/join", method = RequestMethod.POST)
	public @ResponseBody
	Community joinCommunity(@PathVariable Long id, @RequestBody Long userId) {
		return communityService.joinCommunity(id, userId);
	}

	@RequestMapping(value = "/community/{id}/members", method = RequestMethod.GET)
	public @ResponseBody
	List<User> getCommunityMembers(@PathVariable Long id) {
		return communityService.getCommunityMembers(id);
	}

	@RequestMapping(value = "/community/{id}/getMembership", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, Object> getMembership(@PathVariable Long id,
			@RequestParam("userId") Long userId) {

		return communityService.getMembership(id, userId);
	}

	/*
	 * STUFF methods
	 */

	@RequestMapping(value = "/community/{id}/stuff/new")
	public @ResponseBody
	Stuff createStuff(@RequestBody Map<String, Object> jsonObj) {
		Stuff s = new ObjectMapper().convertValue(jsonObj.get("stuff"),
				Stuff.class);
		Long userId = new ObjectMapper().convertValue(jsonObj.get("userID"),
				Long.class);

		return communityService.createStuff(s, userId);
	}

	@RequestMapping(value = "/community/{id}/stuff/list", method = RequestMethod.GET)
	public @ResponseBody
	List<Stuff> listStuffs(@PathVariable("id") Long id) {
		return communityService.getStuffs(id);
	}

	@RequestMapping(value = "/community/{id}/stuff/{stuffId}", method = RequestMethod.GET)
	public @ResponseBody
	Stuff getStuffById(@PathVariable("id") Long id,
			@PathVariable("stuffId") Long stuffId) {
		return communityService.getStuff(id, stuffId);
	}

	@RequestMapping(value = "/community/{id}/stuff/{stuffId}/contribute", method = RequestMethod.POST)
	public @ResponseBody
	StuffContribution contributeToStuff(@RequestBody StuffContribution sc) {
		return communityService.contributeToStuff(sc);
	}

	/*
	 * SERVICE methods
	 */

	@RequestMapping(value = "/community/{id}/service/new")
	public @ResponseBody
	Service createService(@RequestBody Map<String, Object> jsonObj) {
		Service s = new ObjectMapper().convertValue(jsonObj.get("service"),
				Service.class);
		Long userId = new ObjectMapper().convertValue(jsonObj.get("userID"),
				Long.class);

		return communityService.createService(s, userId);
	}

	@RequestMapping(value = "/community/{id}/service/list", method = RequestMethod.GET)
	public @ResponseBody
	List<Service> listServices(@PathVariable("id") Long id) {
		return communityService.getServices(id);
	}

	@RequestMapping(value = "/community/{id}/service/{serviceId}", method = RequestMethod.GET)
	public @ResponseBody
	Service getServiceById(@PathVariable("id") Long id,
			@PathVariable("serviceId") Long serviceId) {
		return communityService.getService(id, serviceId);
	}
	
	@RequestMapping(value = "/community/{id}/service/{serviceId}/contribute", method = RequestMethod.POST)
	public @ResponseBody
	ServiceContribution contributeToService(@RequestBody ServiceContribution sc) {
		return communityService.contributeToService(sc);
	}


	/*
	 * EVENT methods
	 */

	@RequestMapping(value = "/community/{id}/event/new")
	public @ResponseBody
	Event createEvent(@RequestBody Map<String, Object> jsonObj) {
		Event e = new ObjectMapper().convertValue(jsonObj.get("event"),
				Event.class);
		Long userId = new ObjectMapper().convertValue(jsonObj.get("userID"),
				Long.class);

		return communityService.createEvent(e, userId);
	}

	@RequestMapping(value = "/community/{id}/event/list", method = RequestMethod.GET)
	public @ResponseBody
	List<Event> listEvents(@PathVariable("id") Long id) {
		return communityService.getEvents(id);
	}

	@RequestMapping(value = "/community/{id}/event/{eventId}", method = RequestMethod.GET)
	public @ResponseBody
	Event getEventById(@PathVariable("id") Long id,
			@PathVariable("eventId") Long eventId) {
		return communityService.getEvent(id, eventId);
	}

	@RequestMapping(value = "/community/{id}/event/{eventId}/attend", method = RequestMethod.POST)
	public @ResponseBody
	Event attendEvent(@PathVariable("id") Long id,
			@PathVariable("eventId") Long eventId, @RequestBody Long userId) {
		return communityService.attendEvent(id, eventId, userId);
	}

}
